<template>
	<view class="good-list-container">
    <navigator
      :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id"
      v-for="item of goodsList"
      :key="item.goods_id"
      class="good-item-wrapper">
      	<good-item :goodInfoObj="item"></good-item>
    </navigator >
	</view>
</template>

<script>
	export default {
		data() {
			return {
        // 获取请求商品列表数据
        queryGoodListData: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 15
        },
        // 共搜索到的列表数据
        goodTotal: '',
        // 商品列表数据
        goodsList: [],
        // 当前加载状态
        isLoading: false
			};
		},
    onLoad(option) {
      // 接收路由传递参数
      this.queryGoodListData.cid = option.cid || ''
      this.queryGoodListData.query = option.query || ''
      this.getGoodList(tip => uni.$showMsg('共有'+this.goodTotal+'件商品', 3000))
    },
    methods: {
      // 定义获取商品列表的
      async getGoodList(tip, clear) {
        this.isLoading = true
        const { data } = await uni.$http.get('/api/public/v1/goods/search', this.queryGoodListData)
        if (data.meta.status !==200) return uni.$showMsg()
        clear && clear()
        this.goodsList = [...this.goodsList, ...data.message.goods]
        this.goodTotal = data.message.total
        tip && tip()
        this.isLoading = false
      }
    },
    onReachBottom() {
      if (this.goodsList.length >= this.goodTotal) return uni.$showMsg('没有更多啦...')
      if (this.isLoading) return
      this.queryGoodListData.pagenum += 1 
      this.getGoodList()
    },
    onPullDownRefresh() {
      this.queryGoodListData.pagenum = 1
      this.getGoodList(tip => {
        uni.stopPullDownRefresh()
        uni.$showMsg('刷新成功...', 3000)
        }, clear => this.goodsList = [])
    }
	}
</script>

<style lang="scss">
</style>
